package org.leetcode.middle.leetcode55;

public class Solution {

    public boolean canJump3(int[] nums) {
        int n = nums.length;
        int maxCanJump =nums[0];
        for (int i = 0; i <=maxCanJump; i++) {
            maxCanJump=Math.max(maxCanJump,i+nums[i]);
            if (maxCanJump>=n-1){
                return true;
            }
        }

        return false;
    }

    public boolean canJump2(int[] nums){

        if (nums.length==1){
            return true;
        }
        //当前跳跃下最大的覆盖范围
        int coverRange = 0;
        for (int i = 0; i <=coverRange; i++) {

            //每次选取最大的跳跃覆盖范围
            coverRange=Math.max(coverRange,i+nums[i]);

            if (coverRange>=nums.length-1){
                return true;
            }
        }
        return false;
    }


    public boolean canJump(int[] nums) {

        if (nums.length==1)
        {
            return true;
        }

        int coverRange=0;
        for (int i = 0; i <=coverRange; i++) {
            coverRange=Math.max(coverRange,i+nums[i]);

            if (coverRange>=nums.length-1)
            {
                return true;
            }
        }

        return false;
    }
}
